﻿using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;

namespace HamcoWcs.Service.Common
{
    public static class HttpHelper
    {
        public static string PostUrl(string url, string postData)
        {
            string result = "";
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "application/json";
                req.Timeout = 5000;//请求超时时间
                byte[] data = Encoding.UTF8.GetBytes(postData);
                req.ContentLength = data.Length;
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                    reqStream.Close();
                }

                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Stream stream = resp.GetResponseStream();

                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
            }
            catch
            {

            }
            return result;
        }

        public static HttpResponseMessage GetjsonResponse(string ReturnJson)
        {
            HttpResponseMessage result;
            result = new HttpResponseMessage { Content = new StringContent(ReturnJson, Encoding.GetEncoding("UTF-8"), "application/json") };

            return result;
        }
    }
}
